﻿using System;
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class User
    {
        private string[,] _users;
        private MySqlConnection conn;


        public bool isOnline(string id)
        {
            bool online = false;
            string[] result;

            result = Query.SelectQuery(conn, "users", "online", "id=" + id);

            if (result[0] == "1")
                online = true;

            return online;
        }

        public void setOnlineState(string id, bool setOnline)
        {
            if (setOnline)
            {
                Query.UpdateQuery(conn, "users", "online", "1", "id=" + id);
            }
            else
            {
                Query.UpdateQuery(conn, "users", "online", "0", "id=" + id);
            }
        }

        public void banPlayer(string reason, string id)
        {
            string[] columns = new string[2];
            columns[0] = "banreason";
            columns[1] = "ban";

            string[] values = new string[2];
            values[0] = reason;
            values[1] = "1";

            Query.UpdateQuery(conn,"users",columns,values,"id=" + id);
            
        }

        public void unbanPlayer(string id)
        {
            string[] columns = new string[2];
            columns[0] = "banreason";
            columns[1] = "ban";

            string[] values = new string[2];
            values[0] = "";
            values[1] = "0";

            Query.UpdateQuery(conn, "users", columns, values, "id=" + id);
        }

        public bool doesExists(string id)
        {
            bool exists = false;
            int returncount = 0;

            returncount = Query.getRowCount(conn, "users", "id=" + id);

            if (returncount == 1)
                exists = true;

            return exists;
        }

        public bool rightInfo(string id, string password)
        {
            bool rightInfo = false;
            int count = 0;

            count = Query.getRowCount(conn,"users","(id=" + id +") AND (password=" + password + ")");

            if (count == 1)
                rightInfo = true;

            return rightInfo;
        }

        public void login(string id, string password)
        {
            if (!doesExists(id))
            {
                Exception ex = new Exception("User does not exists.");
                throw ex;
            }

            if (rightInfo(id, password))
                setOnlineState(id, true);

        }

        public void setSilk(string id, int newCount,bool prem = false)
        {
            if (!prem)
            {
                Query.UpdateQuery(conn, "users", "silk", newCount.ToString(), "id=" + id);
            }
            else
            {
                Query.UpdateQuery(conn, "users", "premsilk", newCount.ToString(), "id=" + id);
            }

        }

        public int getSilk(string id, bool prem = false)
        {
            int silk = 0;
            string[] result;

            if (!prem)
            {
                result = Query.SelectQuery(conn, "users", "silk", "id=" + id);
            }
            else
            {
                result = Query.SelectQuery(conn, "users", "premsilk", "id=" + id);
            }

            Int32.TryParse(result[0], out silk);

            return silk;
        }

        public void setGold(string id, int newCount)
        {
            Query.UpdateQuery(conn, "users", "gold", newCount.ToString(), "id=" + id);
        }

        public int getGold(string id)
        {
            int gold = 0;
            string[] result;

            result = Query.SelectQuery(conn, "users", "gold", "id=" + id);

            Int32.TryParse(result[0], out gold);

            return gold;
        }

        public int getJob(string id)
        {
            int job = 0;
            string[] result;

            result = Query.SelectQuery(conn, "users", "jobtype", "id=" + id);

            Int32.TryParse(result[0], out job);

            return job;
        }

        public void setJob(string id, int newJob)
        {
            Query.UpdateQuery(conn, "users", "jobtype", newJob.ToString(), "id=" + id);
        }

        public void setWarehouseSlots(string id, int newSlotsCount)
        {
            Query.UpdateQuery(conn, "users", "warehouse_slots", newSlotsCount.ToString(), "id=" + id);
        }

        public int getWarehouseSlots(string id)
        {
            int slots = 0;
            string[] result;

            result = Query.SelectQuery(conn, "users", "warehouse_slots", "id=" + id);

            Int32.TryParse(result[0], out slots);

            return slots;
        }

        public int getCheckInfo(string id)
        {
            int info = 0;
            string[] result;

            result = Query.SelectQuery(conn, "users", "checkinfo", "id=" + id);

            Int32.TryParse(result[0], out info);

            return info;
        }

        public int getUserId(string id)
        {
            int uid = 0;
            string[] result;

            result = Query.SelectQuery(conn, "users", "userid", "id=" + id);

            Int32.TryParse(result[0], out uid);

            return uid;
        }

        public int getIdByName(string name)
        {
            int id;

            for (int i = 0; i < _users.GetLength(0); i++)
            {
                if (_users[i, 2] == name)
                {
                    Int32.TryParse(_users[i, 0], out id);
                    return id;
                }
            }

            return 0;
        }

        public void refresh(MySqlConnection conn)
        {
            string[] columns = new string[1];
            columns[0] = "*";

            _users = Query.SelectQuery(conn, "user", columns);
        }

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }
    }
}
